<?php

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use models\Market;
use models\Commodity;
use models\Menu;
use utils\out;


/**菜品详情页面*/
$app->get('/commodity/market/{id}', function (Application $app, $id) {
    return $app['twig']->render('commodities.html', array("marketId" => $id));
});

/**菜详情页面*/
$app->get('/commodity/{cid}/type/{typeId}', function (Application $app, Request $request, $cid, $typeId) {
    $name = $request->get('name');
	$average = $request->get('price');

	if(mb_strlen($average,'utf8') > 4) {
		$average = substr($average, 0, 4);
	}
	$average = $average .'元／500g';
	
//  return $app['twig']->render('commodity_detail.html',
//      array("cid" => $cid, "typeId" => $typeId, "name"=>$name, "average"=>$average));
	return $app->json(["status"=>'ok',"cid" => $cid, "typeId" => $typeId, "name"=>$name, "average"=>$average]);	
});

/** 菜价列表 **/
$app->get('/commodity/{cid}/type/{typeId}/data', function (Application $app, Request $request, $cid, $typeId) {
	$name = $request->get('name');

    // 菜场
	//$markets =  Market::get();

	// 各菜场菜价
	$prices =  Commodity::getPrices($cid, $typeId);

	// 最佳搭配


	// 推荐菜单
    $menus = Menu::getByStaff($app['db'], $name);

	$result = array(
			'status' => 'ok',
			'result' => array("prices"=>$prices, "menus" => $menus)
		);
	return $app->json($result, 200);
});